var items = [];

var prepareContent = function(){
	addItem('P8040012.JPG', 'sweden, 2014');
	addItem('P8040051.JPG', 'stockholm, sweden, 2014');
	addItem('P8040234.JPG', 'vasa museum, stockholm, sweden, 2014');
	addItem('P8040317.JPG', 'jacek, stockholm, sweden, 2014');
	addItem('P8040384.JPG', 'stockholm, sweden, 2014');
	addItem('P8050506.JPG', 'stockholm, sweden, 2014');
	addItem('P8050678.JPG', 'skansen zoo, stockholm, sweden, 2014');
	addItem('P8060972.JPG', 'stockholm, sweden, 2014');
	addItem('P8100088.JPG', 'łeba, baltic sea, poland, 2014');
	addItem('P8100169.JPG', 'jacek, łeba, baltic sea, poland, 2014');
	addItem('P8110285.JPG', 'justyna, paulina, marzena, łeba, baltic sea, poland, 2014');
	addItem('P8110305.JPG', 'jacek, łeba, baltic sea, poland, 2014');
	addItem('P8120330.JPG', 'jacek, łeba, poland, 2014');
	addItem('01_2012_justyna_i_gumiaki_itp_(11).jpg', 'justyna, 2012');
	addItem('01_2012_justyna_i_gumiaki_itp_(7).jpg', 'justyna, 2012');
	addItem('02_20140526_225614.jpg', 'brussels, belgium, 2014');
	addItem('03_DSC_0005.JPG', 'karomile, 2013');
	addItem('03_IMG_0133.jpg', 'karomile, 2006');
	addItem('04_P3090009.JPG', 'jacek, dad, mateusz, 2014');
	addItem('04P3090031.JPG', 'mateusz, mom, 2014');
	addItem('05_DSC_0228.JPG', 'palace of culture and science, warsaw, poland, 2012');
	addItem('05_P5100172.JPG', 'air show, janów, poland, 2014');
	addItem('02_20140617_154544.jpg', 'karomile, warsaw, poland, 2014');
	addItem('02_20140607_213518.jpg', 'karomile, 2014');
	addItem('06_20140604_200531.jpg', 'asia and zosia, 2014');
	addItem('06_20140604_200540.jpg', 'asia and zosia, 2014');
	addItem('07_IMG_3563.JPG', 'tatra, poland, 2009');
	addItem('08_IMG032.jpg', 'magda, siedlce, poland, 2013');
	addItem('09_P6220041.JPG', 'ola, 2014');
	addItem('09_P6220046.JPG', 'ola, 2014');
	addItem('07_IMG_5569.JPG', 'magda, baltic sea, poland, 2010');
	addItem('10_DSC_0142_(4).JPG', 'aunt magda, dad, grandparents, 2012');
	addItem('10_DSC_0144_(2).JPG', 'parents, aunt magda, grandparents, 2012');
	addItem('08_IMG457.jpg', 'katowice hotel, katowice, poland, 2012');
	addItem('11_P6280304.JPG', 'jacek, łódź, poland, 2014');
	addItem('11_P6280311.JPG', 'jacek, łódź, poland, 2014');
	addItem('12_DSC_0062_(5).JPG', 'jacek, 2013');
	addItem('12_DSC_0066_(4).JPG', 'jacek, 2013');
	addItem('13_P5030582.JPG', 'jacek, polańczyk, bieszczady, poland, 2014');
	addItem('13_P5030583.JPG', 'jacek, polańczyk, bieszczady, poland, 2014');
	addItem('14_P4210215.JPG', 'jacek, justyna, mateusz, 2014');
	addItem('14_P4210216.JPG', 'mom, justyna, mateusz, jacek, 2014');
	addItem('15_DSC_0209_(2).JPG', 'ola, magda, amelka, ania, monika, 2012');
	addItem('15_DSC_0220.JPG', 'ola, 2012');
	addItem('16_IMG_38472.jpg', 'aneta, warsaw, poland, 2013');
	addItem('16_IMG_7944.JPG', '2010');
	addItem('17_P6190272.JPG', 'jacek, liw, poland, 2014');
	addItem('17_P6190287.JPG', 'liw, poland, 2014');
	addItem('18_20140719_215139.jpg', 'simba, 2014');
	addItem('18_20140719_215256.jpg', 'simba, 2014');
	addItem('19_P6280230.JPG', 'karomile, łódź, poland, 2014');
	addItem('19_P6280314.JPG', 'łódź, poland, 2014');
	addItem('20_P6190076.JPG', 'jacek, liw castle, poland, 2014');
	addItem('20_P6190255.JPG', 'jacek, karomile, liw castle, poland, 2014');
	addItem('21_P5010032.JPG', 'marzena, solina, bieszczady, poland, 2014');
	addItem('21_P5010115.JPG', 'solina lake, bieszczady, poland, 2014');
	addItem('22_P5010019.JPG', 'solina, bieszczady, poland, 2014');
	addItem('22_P5010203.JPG', 'justyna, solina, bieszczady, poland, 2014');
	addItem('23_P4210098.JPG', 'jacek, mateusz, mom, dad, justyna, 2014');
	addItem('23_P4210227.JPG', 'jacek, mateusz, dad, 2014');
	addItem('24_P4210116.JPG', 'myciek, dad, 2014');
	addItem('24_P4210210.JPG', 'justyna, mom, 2014');
	addItem('25_DSC_0033_(2).JPG', 'justyna, grodzisk, poland, 2012');
	addItem('25_DSC_0057.JPG', 'justyna, 2012');
	addItem('26_P3090043.JPG', 'justyna, 2013');
	addItem('26_Nowy_folder.jpg', 'justyna, karomile, 2012');
	addItem('25_IMG0312.jpg', 'magda, 2013');
	addItem('28_DSC_0059_(4).JPG', 'moma, new york, usa, 2012');
	addItem('27_P2220118.JPG', '2014');
	addItem('27_P2220127.jpg', '2014');
	addItem('29_P6280009.JPG', 'karomile, jacek, marzena, justyna, łódź, poland, 2014');
	addItem('29_P6220235.JPG', 'amelka, piglet, ania, 2014');
	addItem('28_IMG_2161_(2).JPG', 'palace of culture and science, warsaw, poland, 2009');
	addItem('28_P5150140.JPG', 'palace of culture and science, warsaw, poland, 2014');
	addItem('30_P6220230.JPG', 'ania, ola, amelka, piglet, 2014');
	addItem('30_P6280195.JPG', 'jacek, justyna, łódź, poland, 2014');
	addItem('30_P6280338.JPG', 'jacek, karomile, 2014');
	addItem('31_S6305041.JPG', 'kaletka, masuria, poland, 2008');
	addItem('31_S6305042_-_Kopia.JPG', 'kaletka, masuria, poland, 2008');
	addItem('32_S6301208.JPG', 'justyna, 2007');
	addItem('32_S6304921_(2).JPG', 'magda, kaletka, masuria, poland, 2008');
	addItem('33_P6220014.JPG', 'ola, 2014');
	addItem('33_P6220073.JPG', 'ola, 2014');
	addItem('34_20140703_215313.jpg', 'mińsk mazowiecki, poland, 2014');
	addItem('34_20140719_182926.jpg', 'simba, marzena, karomile, 2014');
	addItem('36_P3090054.JPG', 'dąbrowa, poland, 2014');
	addItem('36_P3090059.JPG', 'dąbrowa, poland, 2014');
	addItem('34_P6220142.JPG', 'ola, klopsik, 2014');
	addItem('35_IMG156.jpg', '2013');
	addItem('35_P6220128.JPG', 'ola, klopsik, 2014');
	addItem('35_S6305563.JPG', 'myciek, 2007');
	addItem('37_20140717_191922.jpg', 'mińsk mazowiecki, poland, 2014');
	addItem('39_20140719_183323.jpg', 'simba, 2014');
	addItem('37_S6308812.JPG', 'jacek, cracow, poland, 2008');
	addItem('38_DSC_0172.JPG', 'justyna, mom, granny, dad, 2013');
	addItem('39_DSC_0177_(2).JPG', 'karomile, 2012');
	addItem('40_S6306925.JPG', 'kamila, mrozy, poland, 2007');
	addItem('40_DSC_0106.JPG', 'ola, 2012');
	addItem('38_P6220194.JPG', 'amelka, 2014');
	addItem('41_DSC_0013.JPG', 'jacek, dad, granny, grodzisk, poland, 2013');
	addItem('41_DSC_0020_(4).JPG', 'granny, 2013');
	addItem('42_DSC_0011.JPG', 'jacek, 2012');
	addItem('43_DSC_0985.JPG', 'new york, usa, 2012');
	addItem('42_P22000502.jpg', 'jacek, 2014');
	addItem('43_IMG592.jpg', 'jacek, filip, 2013');
	addItem('44_P3150068.JPG', 'mińsk mazowiecki, poland, 2014');
	addItem('44_P3150082.JPG', 'mińsk mazowiecki, poland, 2014');
	addItem('45_CSC_0099.JPG', 'zosia, 2012');
	addItem('45_CSC_0480.JPG', 'justyna, marzena, prague, czech republic, 2012');
	addItem('46_P6220123.JPG', 'ania, amelka, ola, 2014');
	addItem('46_P6220131.JPG', 'karomile klopsik, 2014');
	addItem('47_P7190065.JPG', 'pułtusk, poland, 2014');
	addItem('47_P7190266.JPG', 'jacek, pułtusk, poland, 2014');
	addItem('48_P6280347.JPG', 'łódź, poland, 2014');
	addItem('48_P7190099.JPG', 'pułtusk castle, poland, 2014');
	addItem('49_P6190292.JPG', 'jacek, węgrów, poland, 2014');
	addItem('49_P7190016.JPG', 'karomile, jacek, pułtusk, poland, 2014');
	addItem('50_DSC_0019.JPG', 'mrozy, poland, 2012');
	addItem('50_DSC_0034.JPG', '2012');
	addItem('51_DSC_0006_(2).JPG', 'justyna, grodzisk, poland, 2012');
	addItem('51_P3090044.JPG', 'jacek, mateusz, dąbrowa, poland, 2014');
	addItem('52_P4210217.JPG', 'mom, justyna, 2014');
	addItem('52_P4210218.JPG', 'mom, justyna, 2014');
	addItem('53_P7190298.JPG', 'karomile, jacek, pułtusk, poland, 2014');
};

var recentlyLoadedIndex = 0;
var PARCEL_SIZE = 10;

var loadNextItemsParcel = function(){
	// console.log("Loading Parcel with index: " + recentlyLoadedIndex);
	
	for(var i = 0; i < PARCEL_SIZE; i++){
		if(recentlyLoadedIndex < items.length){
			displayItem(items[recentlyLoadedIndex].name, items[recentlyLoadedIndex].desc);
			recentlyLoadedIndex++;
		}else{
			// console.log('All items displayed');	
			return;
		}
	}
};
	
var addItem = function(fileName, description){
	items.push(
		{
			name: fileName,
			desc: description
		}
	);
};

var displayItem = function(fileName, description){
	
	$('#gallery').append(
		'<div class="item"> ' +
		'	<img class="photo" src="img/content/' + fileName + '"/> ' + 
        '	<div class="overflow hidden">' +
        '    	<div class="caption-wrapper"> ' +
	    '            <p class="caption">' + description + '</p>' +
	    '    	</div>' +
        '    </div>' + 
        '</div>');
};

var turnOnMouseOverlay = function(){
	$('.item').mouseover(function(){
    	    $(this).find('.overflow').removeClass('hidden');
	    })
        .mouseout(function(){
            $(this).find('.overflow').addClass('hidden');
	    });
};

var preventCopyPaste = function(){
	$('body').bind('copy paste cut drag drop', function (e) {
   		e.preventDefault();
	});
};

$(window).load(function() {
	
	preventCopyPaste();
	prepareContent();
	
	loadNextItemsParcel();
    turnOnMouseOverlay();

    var imgLoad = imagesLoaded( '#gallery', function() {
    	// console.log("applyingMsnry on imagesLoaded");
	  	new Masonry( '#gallery' );
	});
	
	$(document).endlessScroll({
		bottomPixels: 400,
		intervalFrequency: 10,
		fireDelay: 10,
        ceaseFire: function(i) {
          if (i >= Math.ceil(items.length / PARCEL_SIZE)) {
            return true;
          }
        },
        content: function(i) {
			// console.log("inifinite scroll callback: ", i);
	      	loadNextItemsParcel();
	      	turnOnMouseOverlay();
	      	return " ";
		},
	    callback: function (newElements ) {
	    	new Masonry( '#gallery' );
			// console.log("new elements: ", newElements);
		}		
     });
});
